#ifndef GRATE_UTIL_BIT_VECTOR_H
#define GRATE_UTIL_BIT_VECTOR_H

#include "grate/util/common.h"

namespace growth_rate {

class BitVector {
 public:
  explicit BitVector(size_t length)
      : storage_((length + 7) / 8),
        length_(length) {
    clear();
  }
  bool get(size_t index) const {
    DCHECK_LT(index, length_);
    return (storage_[index >> 3] & (1 << (index & 7))) > 0;
  }
  void set(size_t index, bool value) {
    DCHECK_LT(index, length_);
    uint8_t mask = ~(1 << (index & 7));
    storage_[index >> 3] &= mask;
    if (value)
      storage_[index >> 3] |= (1 << (index & 7));
  }
  void clear() {
    storage_.clear();
  }

 private:
  ScopedArray<uint8_t> storage_;
  const size_t length_;
};

}  // namespace growth_rate

#endif  // GRATE_UTIL_BIT_VECTOR_H
